var GlobalObject = {
    stringToObject: function (keyValues) {
        var splitArray = keyValues.split('&');
        var params = {};
        for (var i = 0; i < splitArray.length; i++) {
            var tempArray = splitArray[i].split("=");
            params[tempArray[0]] = tempArray[1];

        }

        return params;

    },
    ajaxOperation: function (url, actionName, dataParam, ajaxDescrition, Succesfn, isAsync, type) {

        var resData;
        url = $.trim(url);
        actionName = $.trim(actionName);
        ajaxDescrition = $.trim(ajaxDescrition);

        if (((url == null) || (url == "") || !url) || ((actionName == null) || (actionName == "") || !actionName)) {
            return "Ajax请求url和actionName入参不允许为空!";

        }
        if (((dataParam == null) || (dataParam == "") || !dataParam)) {
            dataParam = {action: actionName};
        } else {
            dataParam["action"] = actionName;
        }

        //var typeParam=(type?type:'post');
        var isAsyncParam = ((isAsync && (isAsync != null) && (isAsync != "")) ? isAsync : false);
        var SuccesfnIsFunction = (Succesfn && (typeof Succesfn == "function"));//检测入参是否为函数
        $.ajax({
            url: url, // 跳转到 action
            data: dataParam,
            timeout: (1000 * 60 * 3),
            type: (type ? type : 'post'),
            cache: false,
            dataType: 'json',
            async: isAsyncParam,
            success: function (data) {
                if (data && data.success) {
                    resData = data;
                    SuccesfnIsFunction ? Succesfn(resData) : null;


                } else {
                    $.bootstrapGrowl(ajaxDescrition + ":" + data.message, '', {
                        type: 'danger',
                        align: 'center',
                        width: 'auto',
                        allow_dismiss: true
                    });

                }

            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {

                $.bootstrapGrowl("远程" + ajaxDescrition + "失败 ", '', {
                    type: 'danger',
                    align: 'center',
                    width: 'auto',
                    allow_dismiss: true
                });

            }
        });

        if (((!isAsyncParam) | SuccesfnIsFunction)) {
            return resData
        }
        ;//如果为同步方式调用返回数据


    },
    //获取Get-URL传参 name:获取某个参数名对应的内容
    GetQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return "";
    },
    //获取当前对象坐标
    getElementXY: function (obj) {
        var x = 0, y = 0;
        if (obj.getBoundingClientRect) {
            var box = obj.getBoundingClientRect();
            var D = document.documentElement;
            x = box.left + Math.max(D.scrollLeft, document.body.scrollLeft)
                - D.clientLeft;
            y = box.top + Math.max(D.scrollTop, document.body.scrollTop)
                - D.clientTop
        } else {
            for (; obj != document.body; x += obj.offsetLeft, y += obj.offsetTop, obj = obj.offsetParent) {
            }
        }
        return {
            x: x,
            y: y
        }
    },

    //根据用户名获取用户信息
    getUserInfo: function (rybm, jgbm) {
        var dataParam = {
            rybm: rybm,
            jgbm: jgbm
        };
        resdata = this.ajaxOperation('../../../CommonControl/action.do', "getUserInfo", dataParam, "获取用户信息", function () {

        });
        return resdata;
    },
    //获取url中的参数
    getUrlParam: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return decodeURI(r[2]);
        return null; //返回参数值
    },
    //获取当天相隔天数的日期 AddDayCount 可为整数和负数
    getDateStr: function (AddDayCount, day) {


        var dd = new Date();
        if (day) {
            dd = new Date(day);
        }
        dd.setDate(dd.getDate() + parseInt(AddDayCount, 10));//获取AddDayCount天后的日期 
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;//获取当前月份的日期 
        var d = dd.getDate();
        if (parseInt(d) < 10) {
            d = '0' + d;
        }
        return y + "-" + m + "-" + d;
    },
    //获取数据库系统时钟信息 返回数据.NOWDATE 当前日期不包含时分秒  返回数据.NOWHH 小时
    getNowTimeInfo: function () {

        var resdata = this.ajaxOperation('../../../CommonControl/action.do', "getNowTimeInfo", null, "获取数据库系统时钟信息", function () {

        });
        return resdata.data[0];
    },
    loadXML: function (xmlString) {
        var xmlDoc = null;
        //判断浏览器的类型
        //支持IE浏览器 
        if (!window.DOMParser && window.ActiveXObject) {   //window.DOMParser 判断是否是非ie浏览器
            var xmlDomVersions = ['MSXML.2.DOMDocument.6.0', 'MSXML.2.DOMDocument.3.0', 'Microsoft.XMLDOM'];
            for (var i = 0; i < xmlDomVersions.length; i++) {
                try {
                    xmlDoc = new ActiveXObject(xmlDomVersions[i]);
                    xmlDoc.async = false;
                    xmlDoc.loadXML(xmlString); //loadXML方法载入xml字符串
                    break;
                } catch (e) {
                }
            }
        }
        //支持Mozilla浏览器
        else if (window.DOMParser && document.implementation && document.implementation.createDocument) {
            try {
                /* DOMParser 对象解析 XML 文本并返回一个 XML Document 对象。
                 * 要使用 DOMParser，使用不带参数的构造函数来实例化它，然后调用其 parseFromString() 方法
                 * parseFromString(text, contentType) 参数text:要解析的 XML 标记 参数contentType文本的内容类型
                 * 可能是 "text/xml" 、"application/xml" 或 "application/xhtml+xml" 中的一个。注意，不支持 "text/html"。
                 */
                domParser = new DOMParser();
                xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
            } catch (e) {
            }
        } else {
            return null;
        }

        return xmlDoc;
    },
    //日期格式化
    formatDate: function (date, splitChar) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();
        return y + splitChar + (m < 10 ? ("0" + m) : m) + splitChar + (d < 10 ? ("0" + d) : d);
    },
    //获取客户端当前日期
    getNowdate: function () {
        var tody = new Date();
        var nian = tody.getFullYear();
        var month = tody.getMonth() + 1;
        var day = tody.getDate();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        var jt = nian + "-" + month + "-" + day;
        return jt;
    },
    //  
    greepData: function (data, num, col) {
        if (!col) {
            col = 6;
        }
        var temp = $.grep(data, function (n, i) {
            return ((i + 1) % col == num);
        });
        return temp;
    }
}

//==============================================
//Base64
//==============================================
function Base64() {
    // private property
    _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    //将url地址的传参部分转为base64编码格式，再返回转换后的url地址
    this.ChangeUrlParamToBase64 = function (inputUrl) {

        var output = "";
        var arr = inputUrl.split("?");

        if (arr == null || arr.length <= 1) {
            return "";
        }
        var paramStr = "";

        for (var i = 1; i < arr.length; i++) {
            if (paramStr == "") {
                paramStr = arr[i].toString();
            } else {
                paramStr += "?" + arr[i].toString();
            }

        }
        output = arr[0] + "?" + this.encode(paramStr);
        return output;
    }

    //获取url传参参数
    this.GetParamStrByBase64Url = function (inputUrl, paramName, isBase64) {
        var output = "";
        var arr = inputUrl.split("?");
        if (arr == null || arr.length <= 1) {
            return "";
        }
        var params = arr[1];
        if (isBase64) {
            params = this.decode(arr[1]);
        }

        var paraString = params.split("&");
        var paraObj = {}
        for (i = 0; j = paraString[i]; i++) {
            paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
        }
        var returnValue = paraObj[paramName.toLowerCase()];
        if (typeof (returnValue) == "undefined") {
            return "";
        } else {
            return returnValue;
        }
        return output;
    }

    this.GetParamStrByNoBase64Url = function (url, paramName) {
        var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
        var paraObj = {}
        for (i = 0; j = paraString[i]; i++) {
            paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
        }
        var returnValue = paraObj[paramName.toLowerCase()];
        if (typeof (returnValue) == "undefined") {
            return "";
        } else {
            return returnValue;
        }
    }

    // public method for encoding
    this.encode = function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        input = _utf8_encode(input);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output +
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
        }


        return output;
    }

    // public method for decoding
    this.decode = function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
            enc1 = _keyStr.indexOf(input.charAt(i++));
            enc2 = _keyStr.indexOf(input.charAt(i++));
            enc3 = _keyStr.indexOf(input.charAt(i++));
            enc4 = _keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = _utf8_decode(output);
        return output;
    }

    // private method for UTF-8 encoding
    _utf8_encode = function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext;
    }

    // private method for UTF-8 decoding
    _utf8_decode = function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while (i < utftext.length) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}